package Server.Poker.SSZ;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;

import Server.Agent.GameConfig;
import Server.Agent.GameDB;
import Server.Model.Base.IRoom;
import Server.Model.Base.IServerUserItem;
import Server.Model.Base.SHMUser;
import Server.Model.Hall.QpGameMgr;
import Server.Model.Hall.dthall;
import redis.protocal.UserRedisInfo;

class SSZTable extends IRoom {

	public static final int US_NULL = 0;
	public static final int US_READY = 1;
	public static final int US_PLAY = 2;

	@Override
	public int GetID() {
		return m_dwTableID;
	};

	@Override
	public int GetCreator() {
		return m_Creator;
	}

	@Override
	public String GetType() {
		return "SSZ";
	};

	@Override
	public void OnClientClose(int uid) {
		OnUserStandUp(uid,false);
	};

	public boolean SendData(int wChairID, String sData) {
		// 群发用户
		if (wChairID == Constants.INVALID_CHAIR) {
			for (short i = 0; i < m_pIUserItem.length; i++) {
				if (m_pIUserItem[i].dwUserID > 0) {
					if (m_pIUserItem[i].isAndroidUser == 0) {
						dthall.getInstance().onSend(m_pIUserItem[i].dwUserID, sData);
					} else {
						// mPlayers[i].SendGameMessage(wSubCmdID, pData,
						// wDataSize, data);
					}
				}
			}

			return true;
		} else {
			if (wChairID >= m_pIUserItem.length) {
				dthall.getInstance().onSend(wChairID, sData);
			} else if (m_pIUserItem[wChairID].dwUserID > 0) {
				if (m_pIUserItem[wChairID].isAndroidUser == 0) {
					dthall.getInstance().onSend(m_pIUserItem[wChairID].dwUserID, sData);
				} else {
				}
				return true;
			}
		}

		return false;
	}

	int m_dwLastDoCmdTime;
	public int fangzhu;
	public int CanSit(int _Uid) {

		if(this.isDwcType()){
			UserRedisInfo info = dthall.getInstance().getSafeUserRedisInfo(_Uid);
			if(info.json.optInt("userRmbType") == 1 && !canUserJoinRentRoom(info.json.optInt("upAgentUid"))){
				return 22;
			}else if(info.json.optInt("userRmbType") == 0 && m_clubOwnerId != 0){
				UserRedisInfo clubInfo = dthall.getInstance().getSafeUserRedisInfo(m_clubOwnerId);
				if(clubInfo.json.optInt("userRmbType") == 1){
					return 23;
				}
			}
		}
		if (m_Creator == _Uid) {
			return 14;
		}	
		SHMUser user = dthall.getInstance().getUserInfo(_Uid);
		if (user != null) {
			if (m_payWay.equalsIgnoreCase("FZPAY")) {
				if(_Uid ==fangzhu){
					if (user.getGem(0) < m_byCostGem * 4) {
						return 11;
					}
				}
			
			}else{
				if (m_payWay.equalsIgnoreCase("AAPAY") )
				{
					if (user.getGem(0) < m_byCostGem) {
						return 11;
					}
				}
			}
		}

		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID == _Uid) {
				SUBCMD_S_OnLine cmd = new SUBCMD_S_OnLine();
				cmd.uid = _Uid;
				cmd.seat = i;
				SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
				return i;
			}
		}
		if (m_locked != 0) {
			return 13;//已经开始 并且房主不让近
		}	
		
		int cntUser = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntUser++;
			}
		}
		if (cntUser >= 4) {
			return 13;
		}
		
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID == 0) {
				return i;
			}
		}

		return 12;
	};

	Long m_lStartTime;
	long needDealTime;
	int m_dwTableID;
	long m_dismissTimer;

	int m_byAgreeDismiss[] = new int[5];

	boolean m_bGameStarted;
	boolean m_bTableStarted;
	int m_byRound;
	int m_byMaxRound;
	int m_byMaxUser;
	int m_wFaPaiDelay; // 发牌延迟
	int m_wRobotSeconds[] = new int[4]; // 等待时长

	public int m_byZhuang;// 当前庄位置

	public int m_byQiangZhuang[] = new int[5]; // 是否抢庄，和抢庄的倍率

	public int m_byCostGem;
	public String m_payWay;
	public String m_fapaiWay;

	public String m_zhuangMode;// 选庄，抢庄，轮庄
	public String m_guize; // 算分规则

	public String m_strParam;
	public String m_strRoomDesc;

	public String[] m_vecParam;

	public int m_vecWinScore[] = new int[4];
	public int m_vecLoseScore[] = new int[4];

	public int m_vecRoundScore[] = new int[4];
	public int m_vecResultScore[] = new int[4];

	public int m_vecIsKaiPaiReady[] = new int[4];

	GameLogic m_GameLogic = new GameLogic();

	String m_curStage;

	int m_bTableCardArray[][] = new int[4][13]; // 桌面扑克 //been 4
	int m_cbHandCardData[][] = new int[4][13];
	int m_locked = 0;// 默认允许进

	IServerUserItem m_pIUserItem[] = new IServerUserItem[4]; // 用户指针

	{
		for (int i = 0; i < m_pIUserItem.length; i++) {
			m_pIUserItem[i] = new IServerUserItem();
		}
	}

	int m_LookOnUserID[] = new int[50]; // 观众

	public int m_Creator;// 房主

	SSZTable() {
		m_bTableStarted = false;
		m_lStartTime = System.currentTimeMillis() / 1000;
		m_bRemove = false;
		m_byCostGem = 0;
		m_payWay = "FZPAY";
		m_fapaiWay = "1ZHANG";

		m_zhuangMode = "WUZHUANG";
		m_guize = "GUIZE_HBL";

		m_byMaxUser = 4;
	}

	void Init(int cbReason) {

		m_dwLastDoCmdTime = 0;
		m_bGameStarted = false;
		m_wFaPaiDelay = 0;

		for (int i = 0; i < 4; i++) {
			m_wRobotSeconds[i] = 0;

			m_byQiangZhuang[i] = 0;
			m_vecIsKaiPaiReady[i] = 0;
			m_vecRoundScore[i] = 0;

			for (int j = 0; j < 13; j++) {
				m_cbHandCardData[i][j] = 0;
			}
		}
		switch (cbReason) {
		case Constants.INIT_TOTAL: {
			m_dismissTimer = 0;
			m_byAgreeDismiss[0] = 0;
			m_byAgreeDismiss[1] = 0;
			m_byAgreeDismiss[2] = 0;
			m_byAgreeDismiss[3] = 0;

			Constants.memset(m_LookOnUserID, 0, Constants.sizeof(m_LookOnUserID)); //

			m_byRound = 0;
			m_byMaxRound = 0;
			m_byZhuang = 0;
			for (int i = 0; i < 4; i++) {
				m_vecWinScore[i] = 0;
				m_vecLoseScore[i] = 0;
				m_vecResultScore[i] = 0;

			}
		}
			break;
		case Constants.INIT_FIRST_ROUND: {

		}
			break;
		case Constants.INIT_NEWROUND: {

		}
			break;
		}
	}

	void KickRobots() {
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {

			}
		}
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {

			}
		}
	}

	boolean HasRobot() {
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {
				return true;
			}
		}
		return false;
	}

	void SendAvatar(int _Uid) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}

		String iter = (dthall.getInstance()).m_mapAvatar.get(_Uid);

		SUBCMD_S_Avatar cmd = new SUBCMD_S_Avatar();
		cmd.m_lUid = _Uid;
		if (iter != null) {
			cmd.szAvatarURL = iter;
		} else {
			cmd.szAvatarURL = "http://img.cjpoker.com/uumj.png";
		}

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

	}

	public void OnUserSit(int _Uid) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 4) {
			m_pIUserItem[bySeatID].cbOnline = Constants.US_NULL;
		}
		SHMUser user = dthall.getInstance().getUserInfo(_Uid);
		if (user != null) {
			for (int i = 0; i < 4; i++) {
				if (m_pIUserItem[i].dwUserID == _Uid) {
					m_wRobotSeconds[i] = 0;
					m_pIUserItem[i].dwUserID = _Uid;
					// m_pIUserItem[i].lUserScore = user.getCoin();
					m_pIUserItem[i].wChairID = i;
					m_pIUserItem[i].dwFaceID = 0;
					m_pIUserItem[i].cbGender = user.getSex();
					m_pIUserItem[i].szNickName = user.getNickName(null);
					m_pIUserItem[i].szIP = user.getIP(null);

					SUBCMD_S_Sit cmd = new SUBCMD_S_Sit();
					// Constants.memset(&cmd, 0, Constants.sizeof(cmd));

					cmd.m_lUid = _Uid;
					cmd.m_wUserChairID = m_pIUserItem[i].wChairID;
					cmd.m_dwTableID = m_dwTableID;
					cmd.m_lUserScore = m_pIUserItem[i].lUserScore;
					cmd.m_cbGender = m_pIUserItem[i].cbGender;
					cmd.m_iAvatar = m_pIUserItem[i].dwFaceID;
					cmd.szNickName = user.getNickName(null);
					cmd.szIP = user.getIP(null);

					SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

					if (m_bGameStarted) {
						SendGameScene(_Uid, Constants.GS_PLAYING);
					} else {
						SendGameScene(_Uid, Constants.GS_FREE);
					}

					SendAvatar(m_pIUserItem[i].dwUserID);

					return;
				}
			}
			for (int i = 0; i < 4; i++) {
				if (m_pIUserItem[i].dwUserID == 0) {
					m_wRobotSeconds[i] = 0;
					m_pIUserItem[i].dwUserID = _Uid;
					m_pIUserItem[i].lUserScore = 1000;
					m_pIUserItem[i].wChairID = i;
					m_pIUserItem[i].dwFaceID = 0;
					m_pIUserItem[i].cbGender = user.getSex();
					m_pIUserItem[i].szNickName = user.getNickName(null);
					m_pIUserItem[i].szIP = user.getIP(null);

					SUBCMD_S_Sit cmd = new SUBCMD_S_Sit();
					// Constants.memset(&cmd, 0, Constants.sizeof(cmd));

					cmd.m_lUid = _Uid;
					cmd.m_wUserChairID = m_pIUserItem[i].wChairID;
					cmd.m_dwTableID = m_dwTableID;
					cmd.m_lUserScore = m_pIUserItem[i].lUserScore;
					cmd.m_cbGender = m_pIUserItem[i].cbGender;
					cmd.m_iAvatar = m_pIUserItem[i].dwFaceID;
					cmd.szNickName = user.getNickName(null);
					cmd.szIP = user.getIP(null);

					SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

					if (m_bGameStarted) {
						SendGameScene(_Uid, Constants.GS_PLAYING);
					} else {
						SendGameScene(_Uid, Constants.GS_FREE);
					}

					return;
				}
			}
		}

		return;
	}

	int FindSeat(long _Uid) {
		for (int bySeatID = 0; bySeatID < 4; bySeatID++) {
			if (m_pIUserItem[bySeatID].dwUserID == _Uid) {
				return bySeatID;
			}
		}
		return Constants.INVALID_CHAIR;
	}

	int GetPlayerCount() {
		int cntPlayer = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntPlayer++;
			}
		}
		return cntPlayer;
	}

	int GetRobotCount() {
		int cntPlayer = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {
				cntPlayer++;
			}
		}
		return cntPlayer;
	}

	public void OnUserStandUp(int _Uid,boolean leaveroom) {

		int wChairID = FindSeat(_Uid);
		
		if(m_bTableStarted == false && wChairID != 0 && leaveroom == true){
			SUBCMD_S_StandUp cmd = new SUBCMD_S_StandUp();
			cmd.m_lUid = _Uid;
			cmd.m_wUserChairID = wChairID;
			cmd.m_dwTableID = m_dwTableID;
			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
		}


		if (m_bTableStarted == false && leaveroom == true) {
			if (wChairID > 0 && wChairID < 4) {
				m_pIUserItem[wChairID].Init();
				QpGameMgr.getInstance().OnRemoveUserRoom(_Uid);
			} else {
			}
		} else {
			if (wChairID < 4) {
				m_pIUserItem[wChairID].cbOnline = Constants.US_OFFLINE;
			}
		}
	}

	public void SetParam(String szParam) {
		m_strParam = szParam;

		m_vecParam = m_strParam.split(";");
	}

	boolean SendGameScene(int _Uid, int bGameStatus) {
		if (m_strParam.length() > 0) {
			SUBCMD_S_Param cmd = new SUBCMD_S_Param();
			cmd.m_dwTableID = m_dwTableID;
			cmd.szParam = m_strParam;
			SendData(_Uid, cmd.getJsonData());
		}
		int cntUser = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntUser++;
			}
		}
		{
			SUBCMD_S_Table cmd = new SUBCMD_S_Table();
			cmd.m_dwTableID = m_dwTableID;// 房间ID
			cmd.m_bGameStarted = m_bGameStarted ? 1 : 0;// 是否开始
			cmd.m_bTableStarted = m_bTableStarted ? 1 : 0;// 是否开始
			cmd.m_byRound = m_byRound;
			cmd.m_byMaxRound = m_byMaxRound;
			cmd.m_cntUser = cntUser;// 玩家
			cmd.m_cntLookon = 1;// 观众
			Constants.memset(cmd.m_seatInfo, 0, Constants.sizeof(cmd.m_seatInfo));

			for (int i = 0; i < 4; i++) {
				cmd.m_seatInfo[i].m_lUid = m_pIUserItem[i].dwUserID;// UID
				cmd.m_seatInfo[i].m_iTotalCoin = m_pIUserItem[i].lUserScore;// COIN
				cmd.m_seatInfo[i].m_NickName = m_pIUserItem[i].szNickName;// NICKNAME
				cmd.m_seatInfo[i].m_iBetCoin = 0;//
				cmd.m_seatInfo[i].m_cbGender = m_pIUserItem[i].cbGender;
				cmd.m_seatInfo[i].m_iAvatar = m_pIUserItem[i].dwFaceID;//
				cmd.m_seatInfo[i].m_cbUserStatus = m_pIUserItem[i].cbUserStatus;// 用户状态
				cmd.m_seatInfo[i].m_iVipLevel = m_pIUserItem[i].cbVipLevel;// VIP
																			// LEVEL
				cmd.m_seatInfo[i].m_iTitle = 0;// 称号
				cmd.m_seatInfo[i].m_isLookon = 0;// 是否观众
				cmd.m_seatInfo[i].szIP = m_pIUserItem[i].szIP;// NICKNAME
			}

			SendData(_Uid, cmd.getJsonData());
			for (int i = 0; i < 4; i++) {
				if (m_pIUserItem[i].dwUserID > 0) {
					SendAvatar(m_pIUserItem[i].dwUserID);
				}
			}
		}

		if (m_bTableStarted == true) {
			{
				SUBCMD_S_STAGE cmd = new SUBCMD_S_STAGE();

				cmd.szStage = m_curStage;
				cmd.needDealTime = (int) needDealTime;
				for (int i = 0; i < 4; i++) {
					for (int k = 0; k < 13; k++) {
						cmd.m_bTableCardArray[i][k] = m_bTableCardArray[i][k]; // 桌面扑克
					}
				}

				SendData(_Uid, cmd.getJsonData());
			}

			if (m_bGameStarted == true) {
				{
					SUBCMD_S_TableBegin cmd = new SUBCMD_S_TableBegin();

					cmd.byRound = m_byRound;
					cmd.byMaxRound = m_byMaxRound;

					for (int i = 0; i < 4; i++) {
						if (m_pIUserItem[i].dwUserID > 0) {
							cmd.bIsReady[i] = m_pIUserItem[i].cbUserStatus;
							cmd.bIsKaiPaiReady[i] = m_vecIsKaiPaiReady[i];
						}
					}
					SendData(_Uid, cmd.getJsonData());
				}
				{

					for (int i = 0; i < 4; i++) {
						if (m_pIUserItem[i].dwUserID > 0) {
							SUBCMD_S_TableSide cmd = new SUBCMD_S_TableSide();
							cmd.bySeatID = i;
							cmd.bIsReady = m_pIUserItem[i].cbUserStatus;
							cmd.bIsKaiPaiReady = m_vecIsKaiPaiReady[i];
							for (int k = 0; k < 13; k++) {
								cmd.byPai[k] = m_bTableCardArray[i][k];
							}
							SendData(_Uid, cmd.getJsonData());
						}
					}
				}
				{
					SUBCMD_S_TableEnd cmd = new SUBCMD_S_TableEnd();

					SendData(_Uid, cmd.getJsonData());
				}
			}
		}

		switch (bGameStatus) {
		case Constants.GS_FREE: // 空闲状态
		{
			// 构造数据

			return true;
		}
		case Constants.GS_PLAYING: // 游戏状态
		{
			// 构造数据

			return true;
		}
		}

		return true;
	}

	void FaPai() {
		m_bTableStarted = true;
		m_bGameStarted = true;

		m_curStage = "STAGE_FAPAI";

		needDealTime = 8;

		if (m_byRound == 0) {
			m_byRound = 1;
		}
		int cbTableCardArray[] = new int[52];
		m_GameLogic.RandCardList(cbTableCardArray, 52); // 计算数组的个数
		for (int i = 0; i < 52; i++) {
			int x = i / 13;
			int y = i % 13;
			m_bTableCardArray[x][y] = cbTableCardArray[i];
		}
		System.out.println("FAPAI : " + m_dwTableID + "_" + m_byRound);
		// System.out.println(IJsonExport.sprintf_BYTE(cbTableCardArray, 52));

		for (int i = 0; i < 4; i++) {
			// System.out.println("POS: " + i);
			// System.out.println(IJsonExport.sprintf_BYTE(m_bTableCardArray[i],
			// 13));
		}
		
		
		SUBCMD_S_FaPai cmd = new SUBCMD_S_FaPai();
		cmd.byRound = m_byRound;
		cmd.byMaxRound = m_byMaxRound;
		for (int i = 0; i < 4; i++) {
			m_vecRoundScore[i] = 0;
			if (m_pIUserItem[i].dwUserID > 0 && (m_pIUserItem[i].cbUserStatus == Constants.US_READY
					|| m_pIUserItem[i].cbUserStatus == Constants.US_PLAY)) {
				m_pIUserItem[i].cbUserStatus = Constants.US_PLAY;
				cmd.vecIsReady[i] = 1;
			} else {
				m_pIUserItem[i].cbUserStatus = Constants.US_NULL;
				cmd.vecIsReady[i] = 0;
			}
		}

		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].cbUserStatus == Constants.US_PLAY) {
				for (int k = 0; k < 13; k++) {
					cmd.byPai[k] = m_bTableCardArray[i][k];
				}

				SendData(m_pIUserItem[i].dwUserID, cmd.getJsonData());
			}
		}
	}
	
	
	

	

	public static int GetNextUser(int bySeatID) {
		return (bySeatID + 1) % 4;
	}

	void KaiPai_HBL() {
		int cbCardType[][] = new int[4][3];
		int vecReadyCard[][][] = new int[4][3][5];
		for (int i = 0; i < 4; i++) {
			vecReadyCard[i][0][0] = m_cbHandCardData[i][0];
			vecReadyCard[i][0][1] = m_cbHandCardData[i][1];
			vecReadyCard[i][0][2] = m_cbHandCardData[i][2];
			m_GameLogic.SortCardList(vecReadyCard[i][0], 3);
			cbCardType[i][0] = m_GameLogic.GetCardType(vecReadyCard[i][0], 3);

			vecReadyCard[i][1][0] = m_cbHandCardData[i][3];
			vecReadyCard[i][1][1] = m_cbHandCardData[i][4];
			vecReadyCard[i][1][2] = m_cbHandCardData[i][5];
			vecReadyCard[i][1][3] = m_cbHandCardData[i][6];
			vecReadyCard[i][1][4] = m_cbHandCardData[i][7];
			m_GameLogic.SortCardList(vecReadyCard[i][1], 5);
			cbCardType[i][1] = m_GameLogic.GetCardType(vecReadyCard[i][1], 5);

			vecReadyCard[i][2][0] = m_cbHandCardData[i][8];
			vecReadyCard[i][2][1] = m_cbHandCardData[i][9];
			vecReadyCard[i][2][2] = m_cbHandCardData[i][10];
			vecReadyCard[i][2][3] = m_cbHandCardData[i][11];
			vecReadyCard[i][2][4] = m_cbHandCardData[i][12];
			m_GameLogic.SortCardList(vecReadyCard[i][2], 5);
			cbCardType[i][2] = m_GameLogic.GetCardType(vecReadyCard[i][2], 5);
		}

		int roundTmpScore[] = new int[4];
		int roundSuanFen[][][] = new int[4][4][3];

		int cntUser = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntUser++;
			}
		}
		if (m_zhuangMode.equalsIgnoreCase("WUZHUANG")) {
			for (int i = 0; i < 4; i++) {
				int byNext = i;
				if (vecReadyCard[i][0][0] == 0) {
					continue;
				}
				for (int j = 0; j < 3; j++) {
					byNext = GetNextUser(byNext);
					if (vecReadyCard[byNext][0][0] == 0) {
						continue;
					}
					int curScore = 0;
					int prizeScore = 0;
					int cntWin = 0;
					int cntLose = 0;
					for (int k = 0; k < 3; k++) {
						int suanfen = 0;
						int ret = m_GameLogic.CompareCard(vecReadyCard[i][k], vecReadyCard[byNext][k], 5);

						if (k == 0) {
							if (ret == 2) {
								cntWin++;
								if (cbCardType[i][0] == Constants.CT_THREE_TIAO) {
									curScore += 3;
									prizeScore += 3;
									suanfen = 3;
								} else {
									curScore += 1;
									suanfen = 1;
								}
							}
							if (ret == 1) {
								cntLose++;
								if (cbCardType[byNext][0] == Constants.CT_THREE_TIAO) {
									curScore += -3;
									prizeScore += -3;
									suanfen = -3;
								} else {
									curScore += -1;
									suanfen = -1;
								}
							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						if (k == 1) {
							if (ret == 2) {
								cntWin++;
								if (cbCardType[i][1] == Constants.CT_HU_LU) {
									curScore += 2;
									prizeScore += 2;
									suanfen = 2;
								} else if (cbCardType[i][1] == Constants.CT_TIE_ZHI) {
									curScore += 8;
									prizeScore += 8;
									suanfen = 8;
								} else if (cbCardType[i][1] == Constants.CT_TONG_HUA_SHUN
										|| cbCardType[i][1] == Constants.CT_KING_TONG_HUA_SHUN) {
									curScore += 10;
									prizeScore += 10;
									suanfen = 10;
								} else {
									curScore += 1;
									suanfen = 1;
								}
							}
							if (ret == 1) {
								cntLose++;
								if (cbCardType[byNext][1] == Constants.CT_HU_LU) {
									curScore += -2;
									prizeScore += -2;
									suanfen = -2;
								} else if (cbCardType[byNext][1] == Constants.CT_TIE_ZHI) {
									curScore += -8;
									prizeScore += -8;
									suanfen = -8;
								} else if (cbCardType[byNext][1] == Constants.CT_TONG_HUA_SHUN
										|| cbCardType[byNext][1] == Constants.CT_KING_TONG_HUA_SHUN) {
									curScore += -10;
									prizeScore += -10;
									suanfen = -10;
								} else {
									curScore += -1;
									suanfen = -1;
								}
							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						if (k == 2) {
							if (ret == 2) {
								cntWin++;
								if (cbCardType[i][2] == Constants.CT_TIE_ZHI) {
									curScore += 4;
									prizeScore += 4;
									suanfen = 4;
								} else if (cbCardType[i][2] == Constants.CT_TONG_HUA_SHUN
										|| cbCardType[i][2] == Constants.CT_KING_TONG_HUA_SHUN) {
									curScore += 5;
									prizeScore += 5;
									suanfen = 5;
								} else {
									curScore += 1;
									suanfen = 1;
								}
							}
							if (ret == 1) {
								cntLose++;
								if (cbCardType[byNext][2] == Constants.CT_TIE_ZHI) {
									curScore += -4;
									prizeScore += -4;
									suanfen = -4;
								} else if (cbCardType[byNext][2] == Constants.CT_TONG_HUA_SHUN
										|| cbCardType[byNext][2] == Constants.CT_KING_TONG_HUA_SHUN) {
									curScore += -5;
									prizeScore += -5;
									suanfen = -5;
								} else {
									curScore += -1;
									suanfen = -1;
								}
							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						roundSuanFen[i][byNext][k] = suanfen;

					}
					if (cntWin == 3) // 通吃
					{
						roundTmpScore[i] += 6 + prizeScore;
					} else if (cntLose == 3) // 通吃
					{
						roundTmpScore[i] += -6 + prizeScore;
					} else {
						roundTmpScore[i] += curScore;
					}
				}
			}
			for (int i = 0; i < 4; i++) {
				int cntHBLWin = 0;
				int cntHBLLose = 0;
				int byNext = i;
				for (int j = 0; j < 3; j++) {
					byNext = GetNextUser(byNext);
					if (roundSuanFen[i][byNext][0] > 0 && roundSuanFen[i][byNext][1] > 0
							&& roundSuanFen[i][byNext][2] > 0) {
						cntHBLWin++;
					}
					if (cntHBLWin == 3) {
						roundTmpScore[i] += 7 * 3;
						int byNext1 = GetNextUser(i);
						int byNext2 = GetNextUser(byNext1);
						int byNext3 = GetNextUser(byNext2);
						if (roundSuanFen[i][byNext1][0] > 0 && roundSuanFen[i][byNext1][1] > 0
								&& roundSuanFen[i][byNext1][2] > 0) {
							roundTmpScore[byNext1] -= 7;
						}
						if (roundSuanFen[i][byNext2][0] > 0 && roundSuanFen[i][byNext2][1] > 0
								&& roundSuanFen[i][byNext2][2] > 0) {
							roundTmpScore[byNext2] -= 7;
						}
						if (roundSuanFen[i][byNext3][0] > 0 && roundSuanFen[i][byNext3][1] > 0
								&& roundSuanFen[i][byNext3][2] > 0) {
							roundTmpScore[byNext3] -= 7;
						}
					}
				}
			}
		} else {
			if (true) {
				int byNext = m_byZhuang;

				for (int j = 0; j < 3; j++) {
					byNext = GetNextUser(byNext);
					if (vecReadyCard[byNext][0][0] == 0) {
						continue;
					}
					int curScore = 0;
					int prizeScore = 0;
					int cntWin = 0;
					int cntLose = 0;
					for (int k = 0; k < 3; k++) {
						int suanfen = 0;
						int ret = m_GameLogic.CompareCard(vecReadyCard[m_byZhuang][k], vecReadyCard[byNext][k], 5);

						if (k == 0) {
							if (ret == 2) {
								cntWin++;
								if (cbCardType[m_byZhuang][0] == Constants.CT_THREE_TIAO) {
									curScore += 3;
									prizeScore += 3;
									suanfen = 3;
								} else {
									curScore += 1;
									suanfen = 1;
								}
							}
							if (ret == 1) {
								cntLose++;
								if (cbCardType[byNext][0] == Constants.CT_THREE_TIAO) {
									curScore += -3;
									prizeScore += -3;
									suanfen = -3;
								} else {
									curScore += -1;
									suanfen = -1;
								}
							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						if (k == 1) {
							if (ret == 2) {
								cntWin++;
								if (cbCardType[m_byZhuang][1] == Constants.CT_HU_LU) {
									curScore += 2;
									prizeScore += 2;
									suanfen = 2;
								} else if (cbCardType[m_byZhuang][1] == Constants.CT_TIE_ZHI) {
									curScore += 8;
									prizeScore += 8;
									suanfen = 8;
								} else if (cbCardType[m_byZhuang][1] == Constants.CT_TONG_HUA_SHUN
										|| cbCardType[m_byZhuang][1] == Constants.CT_KING_TONG_HUA_SHUN) {
									curScore += 10;
									prizeScore += 10;
									suanfen = 10;
								} else {
									curScore += 1;
									suanfen = 1;
								}
							}
							if (ret == 1) {
								cntLose++;
								if (cbCardType[byNext][1] == Constants.CT_HU_LU) {
									curScore += -2;
									prizeScore += -2;
									suanfen = -2;
								} else if (cbCardType[byNext][1] == Constants.CT_TIE_ZHI) {
									curScore += -8;
									prizeScore += -8;
									suanfen = -8;
								} else if (cbCardType[byNext][1] == Constants.CT_TONG_HUA_SHUN
										|| cbCardType[byNext][1] == Constants.CT_KING_TONG_HUA_SHUN) {
									curScore += -10;
									prizeScore += -10;
									suanfen = -10;
								} else {
									curScore += -1;
									suanfen = -1;
								}
							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						if (k == 2) {
							if (ret == 2) {
								cntWin++;
								if (cbCardType[m_byZhuang][2] == Constants.CT_TIE_ZHI) {
									curScore += 4;
									prizeScore += 4;
									suanfen = 4;
								} else if (cbCardType[m_byZhuang][2] == Constants.CT_TONG_HUA_SHUN
										|| cbCardType[m_byZhuang][2] == Constants.CT_KING_TONG_HUA_SHUN) {
									curScore += 5;
									prizeScore += 5;
									suanfen = 5;
								} else {
									curScore += 1;
									suanfen = 1;
								}
							}
							if (ret == 1) {
								cntLose++;
								if (cbCardType[byNext][2] == Constants.CT_TIE_ZHI) {
									curScore += -4;
									prizeScore += -4;
									suanfen = -4;
								} else if (cbCardType[byNext][2] == Constants.CT_TONG_HUA_SHUN
										|| cbCardType[byNext][2] == Constants.CT_KING_TONG_HUA_SHUN) {
									curScore += -5;
									prizeScore += -5;
									suanfen = -5;
								} else {
									curScore += -1;
									suanfen = -1;
								}
							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						roundSuanFen[m_byZhuang][byNext][k] = suanfen;
						roundSuanFen[byNext][m_byZhuang][k] = 0 - suanfen;

					}
					roundTmpScore[m_byZhuang] += curScore;
					roundTmpScore[byNext] -= curScore;
					
					if (cntWin == 3) // 通吃
					{
						roundTmpScore[m_byZhuang] += 6 + prizeScore;
					} else if (cntLose == 3) // 通吃
					{
						roundTmpScore[m_byZhuang] += -6 + prizeScore;
					} else {
						roundTmpScore[m_byZhuang] += curScore;
					}
				}
			}
			if (true) {
				int cntHBLWin = 0;
				int byNext = m_byZhuang;
				for (int j = 0; j < 3; j++) {
					byNext = GetNextUser(byNext);
					if (roundSuanFen[m_byZhuang][byNext][0] > 0 && roundSuanFen[m_byZhuang][byNext][1] > 0
							&& roundSuanFen[m_byZhuang][byNext][2] > 0) {
						cntHBLWin++;
					}
					if (cntHBLWin == 3) {
						roundTmpScore[m_byZhuang] += 7 * 3;
						int byNext1 = GetNextUser(m_byZhuang);
						int byNext2 = GetNextUser(byNext1);
						int byNext3 = GetNextUser(byNext2);
						if (roundSuanFen[m_byZhuang][byNext1][0] > 0 && roundSuanFen[m_byZhuang][byNext1][1] > 0
								&& roundSuanFen[m_byZhuang][byNext1][2] > 0) {
							roundTmpScore[byNext1] -= 7;
						}
						if (roundSuanFen[m_byZhuang][byNext2][0] > 0 && roundSuanFen[m_byZhuang][byNext2][1] > 0
								&& roundSuanFen[m_byZhuang][byNext2][2] > 0) {
							roundTmpScore[byNext2] -= 7;
						}
						if (roundSuanFen[m_byZhuang][byNext3][0] > 0 && roundSuanFen[m_byZhuang][byNext3][1] > 0
								&& roundSuanFen[m_byZhuang][byNext3][2] > 0) {
							roundTmpScore[byNext3] -= 7;
						}
					}
				}
			}
		}
		// System.out.println(IJsonExport.sprintf_BYTE(cbCardType[0], 3));
		// System.out.println(IJsonExport.sprintf_BYTE(cbCardType[1], 3));
		// System.out.println(IJsonExport.sprintf_BYTE(cbCardType[2], 3));
		// System.out.println(IJsonExport.sprintf_BYTE(cbCardType[3], 3));

		// System.out.println(IJsonExport.sprintf_BYTE(roundTmpScore, 4));

		for (int i = 0; i < 4; i++) {
			m_vecRoundScore[i] = roundTmpScore[i];
		}
		SUBCMD_S_SuanFen cmd = new SUBCMD_S_SuanFen();

		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				for (int k = 0; k < 3; k++) {
					cmd.nScore[i][j][k] = roundSuanFen[i][j][k];
				}
			}
		}

		String sData = cmd.getJsonData();
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				SendData(m_pIUserItem[i].dwUserID, sData);
			}
		}

		OnEventGameEnd(Constants.INVALID_CHAIR, Constants.GER_NORMAL);
	}

	void KaiPai_PT() {
		int cbCardType[][] = new int[4][3];
		int vecReadyCard[][][] = new int[4][3][5];
		for (int i = 0; i < 4; i++) {
			vecReadyCard[i][0][0] = m_cbHandCardData[i][0];
			vecReadyCard[i][0][1] = m_cbHandCardData[i][1];
			vecReadyCard[i][0][2] = m_cbHandCardData[i][2];
			m_GameLogic.SortCardList(vecReadyCard[i][0], 3);
			cbCardType[i][0] = m_GameLogic.GetCardType(vecReadyCard[i][0], 3);

			vecReadyCard[i][1][0] = m_cbHandCardData[i][3];
			vecReadyCard[i][1][1] = m_cbHandCardData[i][4];
			vecReadyCard[i][1][2] = m_cbHandCardData[i][5];
			vecReadyCard[i][1][3] = m_cbHandCardData[i][6];
			vecReadyCard[i][1][4] = m_cbHandCardData[i][7];
			m_GameLogic.SortCardList(vecReadyCard[i][1], 5);
			cbCardType[i][1] = m_GameLogic.GetCardType(vecReadyCard[i][1], 5);

			vecReadyCard[i][2][0] = m_cbHandCardData[i][8];
			vecReadyCard[i][2][1] = m_cbHandCardData[i][9];
			vecReadyCard[i][2][2] = m_cbHandCardData[i][10];
			vecReadyCard[i][2][3] = m_cbHandCardData[i][11];
			vecReadyCard[i][2][4] = m_cbHandCardData[i][12];
			m_GameLogic.SortCardList(vecReadyCard[i][2], 5);
			cbCardType[i][2] = m_GameLogic.GetCardType(vecReadyCard[i][2], 5);
		}

		int roundTmpScore[] = new int[4];
		int roundSuanFen[][][] = new int[4][4][3];

		int cntUser = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntUser++;
			}
		}
		// 通比模式
		if (m_zhuangMode.equalsIgnoreCase("WUZHUANG")) {
			for (int i = 0; i < 4; i++) {
				int byNext = i;
				if (vecReadyCard[i][0][0] == 0) {
					continue;
				}
				for (int j = 0; j < 3; j++) {
					byNext = GetNextUser(byNext);
					if (vecReadyCard[byNext][0][0] == 0) {
						continue;
					}
					int curScore = 0;
					int prizeScore = 0;
					int cntWin = 0;
					int cntLose = 0;
					for (int k = 0; k < 3; k++) {
						int suanfen = 0;
						int ret = m_GameLogic.CompareCard(vecReadyCard[i][k], vecReadyCard[byNext][k], 5);

						if (k == 0) {
							if (ret == 2) {
								cntWin++;

								curScore += 1;
								suanfen = 1;

							}
							if (ret == 1) {
								cntLose++;

								curScore += -1;
								suanfen = -1;

							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						if (k == 1) {
							if (ret == 2) {
								cntWin++;

								curScore += 1;
								suanfen = 1;

							}
							if (ret == 1) {
								cntLose++;

								curScore += -1;
								suanfen = -1;

							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						if (k == 2) {
							if (ret == 2) {
								cntWin++;

								curScore += 1;
								suanfen = 1;

							}
							if (ret == 1) {
								cntLose++;

								curScore += -1;
								suanfen = -1;

							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						roundSuanFen[i][byNext][k] = suanfen;

					}

					roundTmpScore[i] += curScore;

				}
			}
		} else {
			if (true) {
				int byNext = m_byZhuang;

				for (int j = 0; j < 3; j++) {
					byNext = GetNextUser(byNext);
					if (vecReadyCard[byNext][0][0] == 0) {
						continue;
					}
					int curScore = 0;
					int prizeScore = 0;
					int cntWin = 0;
					int cntLose = 0;
					for (int k = 0; k < 3; k++) {
						int suanfen = 0;
						int ret = m_GameLogic.CompareCard(vecReadyCard[m_byZhuang][k], vecReadyCard[byNext][k], 5);

						if (k == 0) {
							if (ret == 2) {
								cntWin++;

								curScore += 1;
								suanfen = 1;

							}
							if (ret == 1) {
								cntLose++;

								curScore += -1;
								suanfen = -1;

							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						if (k == 1) {
							if (ret == 2) {
								cntWin++;

								curScore += 1;
								suanfen = 1;

							}
							if (ret == 1) {
								cntLose++;

								curScore += -1;
								suanfen = -1;

							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						if (k == 2) {
							if (ret == 2) {
								cntWin++;

								curScore += 1;
								suanfen = 1;

							}
							if (ret == 1) {
								cntLose++;

								curScore += -1;
								suanfen = -1;

							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						roundSuanFen[m_byZhuang][byNext][k] = suanfen;
						roundSuanFen[byNext][m_byZhuang][k] = 0 - suanfen;

					}
					roundTmpScore[m_byZhuang] += curScore;
					roundTmpScore[byNext] -= curScore;					
				}
			}

		}

		for (int i = 0; i < 4; i++) {
			m_vecRoundScore[i] = roundTmpScore[i];
		}
		SUBCMD_S_SuanFen cmd = new SUBCMD_S_SuanFen();

		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				for (int k = 0; k < 3; k++) {
					cmd.nScore[i][j][k] = roundSuanFen[i][j][k];
				}
			}
		}

		String sData = cmd.getJsonData();
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				SendData(m_pIUserItem[i].dwUserID, sData);
			}
		}

		OnEventGameEnd(Constants.INVALID_CHAIR, Constants.GER_NORMAL);
	}

	// 就绪事件
	protected boolean OnUserReady(int wChairID) {
		if (wChairID >= 4) {
			System.out.printf("Error wChairID (%d)\n", wChairID);
			return false;
		}
		m_wRobotSeconds[wChairID] = 0;
		// 状态判断
		if (m_bGameStarted)
			return false;

		m_pIUserItem[wChairID].cbUserStatus = US_READY;

		SUBCMD_S_Ready cmd = new SUBCMD_S_Ready();
		cmd.m_lUid = m_pIUserItem[wChairID].dwUserID;
		cmd.m_wUserChairID = (short) wChairID;// 玩家座位号

		String sData = cmd.getJsonData();
		SendData(Constants.INVALID_CHAIR, sData);

		return false;
	}

	void OnEventGameStart() {
		Init(Constants.INIT_NEWROUND);

		m_curStage = "STAGE_START";

		SUBCMD_S_Start cmd = new SUBCMD_S_Start();
		cmd.byRound = m_byRound;
		cmd.byMaxRound = m_byMaxRound;
		for (int i = 0; i < 4; i++) {
			m_vecRoundScore[i] = 0;
			if (m_pIUserItem[i].dwUserID > 0 && (m_pIUserItem[i].cbUserStatus == Constants.US_READY
					|| m_pIUserItem[i].cbUserStatus == Constants.US_PLAY)) {
				m_pIUserItem[i].cbUserStatus = Constants.US_PLAY;
				cmd.vecIsReady[i] = 1;
			} else {
				m_pIUserItem[i].cbUserStatus = Constants.US_NULL;
				cmd.vecIsReady[i] = 0;
			}
		}

		String sData = cmd.getJsonData();
		SendData(Constants.INVALID_CHAIR, sData);

		if (m_zhuangMode.equalsIgnoreCase("WUZHUANG")) {
			FaPai();
		} else {
			needDealTime = 0;
			if (m_zhuangMode.equalsIgnoreCase("LUNZHUANG") || m_zhuangMode.equalsIgnoreCase("DINGZHUANG")) {
				needDealTime = 2;
			}
			if (m_zhuangMode.equalsIgnoreCase("QIANGZHUANG")) {
				needDealTime = 8;
			}
		}

		m_bTableStarted = true;
		m_bGameStarted = true;
	}
	public void DoDismissRoomByOverTime(){
		OnEventGameEnd(0, Constants.GAME_END_DISMISS);
	}
	void OnEventGameEnd(int bySeatID, int cbReason) {
		m_byRound = m_byRound + 1;
		OnRoundInfoChange();
		m_curStage = "STAGE_JIESUAN";
		needDealTime = 10;

		m_bGameStarted = false;

		ConcludeGame();
		int cntUser = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				cntUser++;
			}
		}

		if (cbReason != Constants.GAME_END_DISMISS) {
			for (int i = 0; i < 4; i++) {
				m_pIUserItem[i].lUserScore += m_vecRoundScore[i];
				m_vecResultScore[i] += m_vecRoundScore[i];
			}

			{
				SUBCMD_S_JieSuan cmd = new SUBCMD_S_JieSuan();
				cmd.byRound = m_byRound;
				for (int i = 0; i < 4; i++) {
					cmd.vecIsReady[i] = m_vecIsKaiPaiReady[i];
					Constants.CopyMemory(cmd.byPai[i], m_cbHandCardData[i], 13);
				}
				for (int i = 0; i < 4; i++) {
					cmd.nRoundScore[i] = m_vecRoundScore[i];
					cmd.nUserScore[i] = m_pIUserItem[i].lUserScore;
				}
				SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

				for (int i = 0; i < 4; i++) {// 清除牌局数据
					m_vecRoundScore[i] = 0;
				}
			}
		}

		if (m_byRound <= 1 && cbReason == Constants.GAME_END_DISMISS) {
			SUBCMD_S_DismissRoom cmd = new SUBCMD_S_DismissRoom();
			cmd.lUid = 0;
			cmd.cbResult = 0;
			cmd.m_wUserChairID = 0;
			cmd.m_dwTableID = m_dwTableID;

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);

		} else if (m_byRound > m_byMaxRound || cbReason == Constants.GAME_END_DISMISS) {
			int before_cost[] = new int[4];
			int after_cost[] = new int[4];

			int cost[] = new int[4];			
			for(int i=0;i<4;i++)
			{
				before_cost[i] = 0;
				cost[i] = 0;
				after_cost[i] = 0;
			}
			
			for (int i = 0; i < 4; i++) 
			{				
				if(m_pIUserItem[i].dwUserID > 0)
				{
					SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
					if(user != null)
					{
						before_cost[i] = user.getGem(0);
					}
				}
			}	
			if (m_byRound > 2) {
				if (m_payWay == "FZPAY") {
					int cost_gem = 0;
					for (int i = 0; i < 4; i++) {
						if (m_pIUserItem[i].dwUserID > 0) {
							cost_gem += m_byCostGem;
						}
					}
					if (m_Creator > 0) {
						SHMUser user = dthall.getInstance().getUserInfo(m_Creator);
						if (user != null && cost_gem < 20) {
							user.getGem(0 - cost_gem);
						}
					} else {
						SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[0].dwUserID);
						if (user != null && cost_gem < 20) {
							user.getGem(0 - cost_gem);
						}
						cost[0] = 0 - cost_gem;
					}
				}
				if (m_payWay == "AAPAY") {
					for (int i = 0; i < 4; i++) {
						int lFangZhu = m_pIUserItem[i].dwUserID;
						if (lFangZhu > 0) {
							SHMUser user = dthall.getInstance().getUserInfo(lFangZhu);
							if (user != null && m_byCostGem < 20) {
								user.getGem(0 - m_byCostGem);
								cost[i] = 0 - m_byCostGem;
							}
						}
					}
				}
				for (int i = 0; i < 4; i++) 
				{				
					if(m_pIUserItem[i].dwUserID > 0)
					{
						SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
						if(user != null)
						{
							after_cost[i] = user.getGem(0);
						}
					}
				}
			}
			SUBCMD_S_REPORT cmd = new SUBCMD_S_REPORT();
			cmd.cbReason = cbReason;
			cmd.whoDismiss = 0;

			for (int k = 0; k < 4; k++) {
				cmd.vecWinScore[k] = m_vecWinScore[k];
				cmd.vecLoseScore[k] = m_vecLoseScore[k];
				cmd.vecResultScore[k] = m_vecResultScore[k];
			}

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			log_counter++;
			log_counter = log_counter % 100;
			Long t = System.currentTimeMillis() / 1000;
			Long battle_id = (t % 1000000) * 100 + log_counter;

			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String created = df.format(new Date());
			{

				JSONObject root = new JSONObject();

				root.put("command", "save_battle_log");
				root.put("battle_id", battle_id);

				root.put("room_type", GetType());

				root.put("room_id", m_dwTableID);
				root.put("creator", GetCreator());

				root.put("jushu", m_byMaxRound);
				root.put("limitscore", 0);

				if (m_payWay == "WINPAY") {
					root.put("payway", 1);
				} else if (m_payWay == "BIGWINPAY") {
					root.put("payway", 2);
				} else {
					root.put("payway", 0);
				}
				root.put("created", created);
				root.put("uid0", (int) m_pIUserItem[0].dwUserID);
				root.put("nickname0", m_pIUserItem[0].szNickName);
				root.put("score0", m_vecResultScore[0]);
				root.put("cost0", cost[0]);
				root.put("uid1", (int) m_pIUserItem[1].dwUserID);
				root.put("nickname1", m_pIUserItem[1].szNickName);
				root.put("score1", m_vecResultScore[1]);
				root.put("cost1", cost[1]);
				root.put("uid2", (int) m_pIUserItem[2].dwUserID);
				root.put("nickname2", m_pIUserItem[2].szNickName);
				root.put("score2", m_vecResultScore[2]);
				root.put("cost2", cost[2]);
				root.put("uid3", (int) m_pIUserItem[3].dwUserID);
				root.put("nickname3", m_pIUserItem[3].szNickName);
				root.put("score3", m_vecResultScore[3]);
				root.put("cost3", cost[3]);
				root.put("uid4", 0);
				root.put("nickname4", "");
				root.put("score4", 0);
				root.put("cost4", 0);

				GameDB.getInstance().push_task(null, root);

				try
				{
					for(int i =0;i<4;i++)
					{
						if(m_pIUserItem[i].dwUserID > 0 && cost[i] != 0)
						{
							JSONObject root_cost = new JSONObject();
							root_cost.put("command", "save_charge_log");
							root_cost.put("created", created);
							root_cost.put("uid", (int) m_pIUserItem[i].dwUserID);
							root_cost.put("before_cost", (int) before_cost[i]);
							root_cost.put("after_cost", (int) after_cost[i]);
							root_cost.put("cost", (int) cost[i]);
							root_cost.put("reason", "用户" + m_pIUserItem[0].dwUserID + "创建  十三水   " + m_byMaxRound + "局 " + m_payWay + " " );
							root_cost.put("room_id", m_dwTableID);
							
							GameDB.getInstance().push_task(null, root_cost);
						}
					}	
				}catch(Exception e)
				{
					e.printStackTrace();
				}
			}

			m_bTableStarted = false;
			for (int i = 0; i < 4; i++) {// 清除牌局数据
				m_vecResultScore[i] = 0;
			}

			DelayRemove();

			QpGameMgr.getInstance().OnDismissRoom(this);

		}

	}

	protected boolean ConcludeGame() {
		// 设置状态

		RepositTable();

		for (int i = 0; i < 4; ++i) {
			if (m_pIUserItem[i].dwUserID > 0) {
				m_pIUserItem[i].cbUserStatus = US_NULL;
			}
		}

		return true;
	}

	// 复位桌子
	public void RepositTable() {
		// 状态变量
		m_bGameStarted = false;

	}

	static int log_counter = 0;

	void OnDealQiangZhuang() {

		m_curStage = "STAGE_QIANGZHUANG";
		needDealTime = 3;

		List<Integer> vecQiangZhuang = new ArrayList<>();
		List<Integer> vecTmpUser = new ArrayList<>();
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				if (m_byQiangZhuang[i] > 0) {
					vecQiangZhuang.add(i);
				}
				vecTmpUser.add(i);
			}
		}
		int byQiangZhuang[] = new int[5];
		SUBCMD_S_DingZhuang cmd = new SUBCMD_S_DingZhuang();
		cmd.lUid = 0;
		cmd.wChairID = 0;
		cmd.wPower = 0;
		if (vecQiangZhuang.size() > 0) {
			int byZhuang = vecQiangZhuang.get(Constants.rand() % vecQiangZhuang.size());
			if (m_byQiangZhuang[byZhuang] == 0) {
				m_byQiangZhuang[byZhuang] = 1;
			}
			cmd.lUid = m_pIUserItem[byZhuang].dwUserID;
			cmd.wChairID = byZhuang;
			cmd.wPower = m_byQiangZhuang[byZhuang];

			for (int i = 0; i < vecQiangZhuang.size(); i++) {
				byQiangZhuang[vecQiangZhuang.get(i)] = m_byQiangZhuang[vecQiangZhuang.get(i)];
			}
			m_byZhuang = byZhuang;
		} else {
			if (vecTmpUser.size() > 0) {
				int byZhuang = vecTmpUser.get(Constants.rand() % vecTmpUser.size());
				if (m_byQiangZhuang[byZhuang] == 0) {
					m_byQiangZhuang[byZhuang] = 1;
				}
				for (int i = 0; i < 4; i++) {
					if (m_pIUserItem[i].dwUserID > 0) {
					} else {
						m_byQiangZhuang[i] = 0;
					}
				}
				cmd.lUid = m_pIUserItem[byZhuang].dwUserID;
				cmd.wChairID = byZhuang;
				cmd.wPower = m_byQiangZhuang[byZhuang];

				for (int i = 0; i < vecTmpUser.size(); i++) {
					byQiangZhuang[vecTmpUser.get(i)] = m_byQiangZhuang[vecTmpUser.get(i)];
					if (byQiangZhuang[vecTmpUser.get(i)] < 0 || byQiangZhuang[vecTmpUser.get(i)] > 250) {
						byQiangZhuang[vecTmpUser.get(i)] = 1;
					}
				}
				m_byZhuang = byZhuang;
			} else {
				return;
			}

		}
		for (int i = 0; i < 4; i++) {
			cmd.byQiangZhuang[i] = byQiangZhuang[i];
		}

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

	}

	void QiangZhuang(int byZhuang,int delay_time) {
		m_curStage = "STAGE_QIANGZHUANG";
		needDealTime = delay_time;

		SUBCMD_S_DingZhuang cmd = new SUBCMD_S_DingZhuang();
		cmd.lUid = 0;
		cmd.wChairID = 0;
		cmd.wPower = 0;

		if (m_byQiangZhuang[byZhuang] == 0) {
			m_byQiangZhuang[byZhuang] = 1;
		}
		cmd.lUid = m_pIUserItem[byZhuang].dwUserID;
		cmd.wChairID = byZhuang;
		cmd.wPower = m_byQiangZhuang[byZhuang];

		m_byZhuang = byZhuang;

		cmd.byQiangZhuang[byZhuang] = 1;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	void OnDealAI(int dt) {
		m_DealTime = (int) (System.currentTimeMillis() / 1000);
		if (m_wFaPaiDelay > 0) {
			m_wFaPaiDelay--;
			return;
		}
		int cntUser = 0;
		for (int wChairID = 1; wChairID < 4; wChairID++) {
			if (m_pIUserItem[wChairID].dwUserID > 0) {
				cntUser++;
			}
		}
		if (m_curStage == "STAGE_START") {
			if (m_zhuangMode.equalsIgnoreCase("QIANGZHUANG")) {
				QiangZhuang(m_byZhuang,3);
			} else if (m_zhuangMode.equalsIgnoreCase("LUNZHUANG")) {
				int byNextUser = GetNextUser(m_byZhuang);
				QiangZhuang(byNextUser,1);
			} else if (m_zhuangMode.equalsIgnoreCase("DINGZHUANG")) {
				QiangZhuang(m_byZhuang,1);
			} else {

			}
		} else if (m_curStage == "STAGE_QIANGZHUANG") {
			FaPai();
		} else if (m_curStage == "STAGE_FAPAI") {

		}
	}

	public void OnDeal(int dt) {
		if (m_bTableStarted == true) {
			if (needDealTime > 0) {
				needDealTime--;
				return;
			}
			if (m_wFaPaiDelay > 0) {
				m_wFaPaiDelay--;
				return;
			}
			if (m_bGameStarted) {
				OnDealAI(dt);
			} else {
				// OnEventGameStart();
			}
		}
	}

	static int randRobotIndex = 999;

	void AddRobot() {
		randRobotIndex++;
		int robot_uid = randRobotIndex % 9999 + 200000;

		Vector<Integer> vecEmpty = new Vector<>();
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID == 0) {
				vecEmpty.add(i);

			}
		}
		if (vecEmpty.size() > 0) {
			int rand = Constants.rand(0, 10000);
			int wChairID = vecEmpty.get(Constants.rand() % vecEmpty.size());

			int vecMaleFace[] = { 1, 3, 5, 7, 9 };
			int vecFemaleFace[] = { 2, 4, 6, 8, 10 };
			m_pIUserItem[wChairID].dwUserID = robot_uid;
			m_pIUserItem[wChairID].lUserScore = 1000;
			m_pIUserItem[wChairID].wChairID = wChairID;
			m_pIUserItem[wChairID].cbGender = rand % 2;
			if (m_pIUserItem[wChairID].cbGender == 1) {
				m_pIUserItem[wChairID].dwFaceID = vecMaleFace[rand % 4];
			} else {
				m_pIUserItem[wChairID].dwFaceID = vecFemaleFace[rand % 4];
			}
			m_pIUserItem[wChairID].cbUserStatus = Constants.US_NULL;
			m_pIUserItem[wChairID].isAndroidUser = 1;

			m_pIUserItem[wChairID].szNickName = "USER[" + wChairID + "]";

			{
				SUBCMD_S_Sit cmd = new SUBCMD_S_Sit();
				// memset(&cmd, 0, sizeof(cmd));

				cmd.m_lUid = robot_uid;
				cmd.m_wUserChairID = m_pIUserItem[wChairID].wChairID;
				cmd.m_lUserScore = m_pIUserItem[wChairID].lUserScore;
				cmd.m_iAvatar = m_pIUserItem[wChairID].dwFaceID;
				cmd.szNickName = m_pIUserItem[wChairID].szNickName;
				cmd.szIP = "";

				SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
			}
			{
				m_pIUserItem[wChairID].cbUserStatus = Constants.US_READY;

				SUBCMD_S_Ready cmd = new SUBCMD_S_Ready();
				// memset(&cmd, 0, sizeof(cmd));

				cmd.m_lUid = robot_uid;
				cmd.m_wUserChairID = m_pIUserItem[wChairID].wChairID;

				SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
			}
			m_wRobotSeconds[wChairID] = 0;
		}
	}

	void Handle_onJoin(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 4) {
			m_pIUserItem[bySeatID].cbOnline = Constants.US_NULL;
		}

		SUBCMD_S_Join cmd = new SUBCMD_S_Join();
		// memset(&cmd, 0, sizeof(cmd));

		String sData = cmd.getJsonData();
		SendData(_Uid, sData);
	}

	void Handle_onReady(int _Uid, String vecArg[]) {
		if (m_bGameStarted == true) {
			return;
		}
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 4) {
			m_pIUserItem[bySeatID].cbUserStatus = Constants.US_READY;
		}
		int cntReady = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].cbUserStatus == Constants.US_READY) {
				cntReady++;
			}
		}

		cntReady = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].cbUserStatus == Constants.US_READY) {
				cntReady++;
			}
		}
		SUBCMD_S_Ready cmd = new SUBCMD_S_Ready();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = bySeatID;
		cmd.m_dwTableID = m_dwTableID;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
		if (cntReady == m_byMaxUser) {
			OnEventGameStart();
		} else {

		}
	}

	void Handle_onRobot(int _Uid, String vecArg[]) {
		if (m_bGameStarted == true) {
			return;
		}
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 4) {
			m_pIUserItem[bySeatID].cbUserStatus = Constants.US_READY;
		}
		int cntPlayer = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntPlayer++;
			}
		}
		if (cntPlayer < 4) {
			for (int i = 0; i < 4 - cntPlayer; i++) {
				AddRobot();
			}
		}
	}

	// 表情
	void Handle_onBiaoQing(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, wChairID);
			return;
		}
		if (vecArg.length < 2)
			return;
		int index = Constants.atoi(vecArg[1]);

		SUBCMD_S_BiaoQing cmd = new SUBCMD_S_BiaoQing();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.m_iBiaoQingIndex = index;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	// 语音
	void Handle_onYuYin(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, wChairID);
			return;
		}
		if (vecArg.length < 2)
			return;
		int index = Constants.atoi(vecArg[1]);

		SUBCMD_S_YuYin cmd = new SUBCMD_S_YuYin();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.m_iYuYinIndex = index;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	// 送礼
	void Handle_onSongLi(int _Uid, String vecArg[]) {
	}

	// 聊天
	void Handle_onChat(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, wChairID);
			return;
		}
		if (vecArg.length < 2)
			return;
		String strContent = vecArg[1];

		SUBCMD_S_Chat cmd = new SUBCMD_S_Chat();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.szContent = strContent;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	// 语音
	void Handle_onVoice(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, wChairID);
			return;
		}
		if (vecArg.length < 3)
			return;
		int seconds = Constants.atoi(vecArg[1]);
		String strVoiceURL = vecArg[2];

		SUBCMD_S_Voice cmd = new SUBCMD_S_Voice();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.seconds = seconds;
		cmd.szVoiceURL = strVoiceURL;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	// 语音
	void Handle_onTryDismissRoom(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		if (vecArg.length < 2)
			return;
		int cntUser = 0;
		int cntOnline = 0;
		int cntOffline = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				cntUser++;
			}
		}
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				if (m_pIUserItem[i].cbOnline == Constants.US_OFFLINE) {
					cntOffline++;
				} else {
					cntOnline++;
				}
			}
		}
		if (cntUser > 1) {
			if (m_dismissTimer == 0) {
				m_dismissTimer = Constants.time(null);
			}
			m_byAgreeDismiss[bySeatID] = 1;

			SUBCMD_S_TryDismissRoom cmd = new SUBCMD_S_TryDismissRoom();
			cmd.lUid = _Uid;
			cmd.dwTableID = m_dwTableID;
			cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			if (cntOnline == 1) {
				OnEventGameEnd(0, Constants.GAME_END_DISMISS);
				DelayRemove();
				QpGameMgr.getInstance().OnDismissRoom(this);

				m_dismissTimer = 0;
			}
		} else {
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);
		}

	}

	// 语音
	void Handle_onAgreeDismissRoom(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		if (vecArg.length < 2)
			return;
		m_byAgreeDismiss[bySeatID] = 1;

		SUBCMD_S_AgreeDismissRoom cmd = new SUBCMD_S_AgreeDismissRoom();
		cmd.lUid = _Uid;
		cmd.dwTableID = m_dwTableID;
		cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

		if (bySeatID == 0 && m_bTableStarted == false) {
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);
			return;
		}
		int cntAgree = 0;
		for (int i = 0; i < 4; i++) {
			if (m_byAgreeDismiss[i] > 0) {
				cntAgree++;
			}
		}
		int cntUser = 0;
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				cntUser++;
			}
		}
		if (cntAgree >= 2 && cntAgree >= cntUser - 1) {
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);

			if (ShouldBeRemoved()) {
				// niuniuTableMgr::instance().DismissTable(this);
			}
		}
	}

	// 语音
	void Handle_onRefuseDismissRoom(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		if (vecArg.length < 2)
			return;

		m_dismissTimer = 0;
		m_byAgreeDismiss[0] = 0;
		m_byAgreeDismiss[1] = 0;
		m_byAgreeDismiss[2] = 0;
		m_byAgreeDismiss[3] = 0;
		m_byAgreeDismiss[4] = 0;

		SUBCMD_S_RefuseDismissRoom cmd = new SUBCMD_S_RefuseDismissRoom();
		cmd.lUid = _Uid;
		cmd.dwTableID = m_dwTableID;
		cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	void Handle_onStart(int _Uid, String vecArg[]) {
		if (vecArg.length < 2)
			return;

		int bySeatID = FindSeat(_Uid);
		if (m_bTableStarted == false && bySeatID == 0 && GetPlayerCount() > 1) {
			OnEventGameStart();
		}
	}

	void Handle_onQiangZhuang(int _Uid, String vecArg[]) {
		if (vecArg.length < 3)
			return;

		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}

		m_byQiangZhuang[bySeatID] = 1;

		int power = 1;

		SUBCMD_S_QiangZhuang cmd = new SUBCMD_S_QiangZhuang();
		cmd.lUid = _Uid;
		cmd.wChairID = bySeatID;
		cmd.wPower = power;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

		int curNum = 0;
		int XuanZhuangN = 0;
		for (int i = 0; i < 4; i++) {
			if (m_byQiangZhuang[i] == 1 || m_byQiangZhuang[i] == -1) {
				XuanZhuangN++;
			}
			if (m_pIUserItem[i].dwUserID > 0) {
				curNum++;
			}
		}
		if (XuanZhuangN >= curNum && m_curStage == "STAGE_START") {
			OnDealQiangZhuang();
		}
	}

	void Handle_onBuQiangZhuang(int _Uid, String vecArg[]) {
		if (vecArg.length < 3)
			return;
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		m_byQiangZhuang[bySeatID] = -1;

		int curNum = 0;
		int XuanZhuangN = 0;
		for (int i = 0; i < 4; i++) {
			if (m_byQiangZhuang[i] == 1 || m_byQiangZhuang[i] == -1) {
				XuanZhuangN++;
			}
			if (m_pIUserItem[i].dwUserID > 0) {
				curNum++;
			}
		}
		if (XuanZhuangN >= curNum && m_curStage == "STAGE_START") {
			needDealTime = 8;
			OnDealQiangZhuang();
		}

	}

	public int FindSeat(int _Uid) {
		for (int bySeatID = 0; bySeatID < 4; bySeatID++) {
			if (m_pIUserItem[bySeatID].dwUserID == _Uid) {
				return bySeatID;
			}
		}
		return Constants.INVALID_CHAIR;
	}

	public int m_DealTime = 0;

	public int getDealTime() {
		return m_DealTime;
	}

	public boolean m_bRemove = false;

	public void DelayRemove() {
		m_bRemove = true;
	}

	public void ResetKillFlag() {
		m_bRemove = false;
	}

	public boolean ShouldBeRemoved() {
		return m_bRemove;
	}

	public void Handle_onLock(Integer lUid, String[] vecArg) {

		if (vecArg.length < 2)
			return;
		if (m_bTableStarted && m_pIUserItem[0].dwUserID == lUid) {// 已经开始 并且是房主
			int index = Constants.atoi(vecArg[1]);
			m_locked = index;
		}
	}

	void Handle_onExpr(int _Uid, String[] vecArg) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		int uid1 = Integer.parseInt(vecArg[1]);
		int uid2 = Integer.parseInt(vecArg[2]);
		int index = Integer.parseInt(vecArg[3]);
		
		SUBCMD_S_ExprMsg cmd = new SUBCMD_S_ExprMsg();
		cmd.m_uid1 = uid1;
		cmd.m_uid2 = uid2;
		cmd.m_index = index;
		
		SendData(Constants.INVALID_CHAIR,cmd.getJsonData());
	}
	void Handle_InBack(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
				
		SUBCMD_S_InBack cmd = new SUBCMD_S_InBack();
		cmd.byActionUser = wChairID;
		SendData(Constants.INVALID_CHAIR,cmd.getJsonData());
	}

	void Handle_InFore(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		
		SUBCMD_S_InFore cmd = new SUBCMD_S_InFore();
		cmd.byActionUser = wChairID;
		SendData(Constants.INVALID_CHAIR,cmd.getJsonData());
	}
	public void Handle_onKaiPaiReady(Integer lUid, String[] vecArg) {

		if (vecArg.length < 2)
			return;
		int wChairID = FindSeat(lUid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		String strTanPai = vecArg[1];
		int dwHandCard[] = new int[13];

		String vecArgTanPai[] = strTanPai.split("\\,");
		if (vecArgTanPai.length != 13) {
			return;
		}

		try {
			for (int i = 0; i < 13; i++) {
				dwHandCard[i] = Integer.parseInt(vecArgTanPai[i]);
			}
			for (int i = 0; i < 13; i++) {
				int cntCard = 0;
				for (int j = 0; j < 13; j++) {
					if (m_bTableCardArray[wChairID][j] == dwHandCard[i]) {
						cntCard++;
					}
				}
				if (cntCard != 1) {
					SUBCMD_S_Error cmd = new SUBCMD_S_Error();
					cmd.byRound = m_byRound;
					cmd.bySeatID = wChairID;
					cmd.errCode = 0;
					cmd.errMsg = "系统认定您配牌出错，请仔细检查您的牌";

					String sData = cmd.getJsonData();
					SendData(m_pIUserItem[wChairID].dwUserID, sData);

					return;
				}
			}
			int card0[] = new int[5];
			int card1[] = new int[5];
			int card2[] = new int[5];

			card0[0] = dwHandCard[0];
			card0[1] = dwHandCard[1];
			card0[2] = dwHandCard[2];
			card0[3] = 0;
			card0[4] = 0;
			card1[0] = dwHandCard[0 + 3];
			card1[1] = dwHandCard[1 + 3];
			card1[2] = dwHandCard[2 + 3];
			card1[3] = dwHandCard[3 + 3];
			card1[4] = dwHandCard[4 + 3];
			card2[0] = dwHandCard[0 + 8];
			card2[1] = dwHandCard[1 + 8];
			card2[2] = dwHandCard[2 + 8];
			card2[3] = dwHandCard[3 + 8];
			card2[4] = dwHandCard[4 + 8];
			m_GameLogic.SortCardList(card0, 3);
			m_GameLogic.SortCardList(card1, 5);
			m_GameLogic.SortCardList(card2, 5);
			int ret1 = m_GameLogic.CompareCard(card0, card1, 5);
			int ret2 = m_GameLogic.CompareCard(card1, card2, 5);

			if (ret1 == 2 || ret2 == 2) {
				SUBCMD_S_Error cmd = new SUBCMD_S_Error();
				cmd.byRound = m_byRound;
				cmd.bySeatID = wChairID;
				cmd.errCode = 0;
				cmd.errMsg = "第二道必须大于第一道小于第三道";

				String sData = cmd.getJsonData();
				SendData(m_pIUserItem[wChairID].dwUserID, sData);

				return;
			}
			for (int i = 0; i < 13; i++) {
				m_cbHandCardData[wChairID][i] = dwHandCard[i];
			}
			// System.out.println(IJsonExport.sprintf_BYTE(m_cbHandCardData[wChairID],
			// 13));
		} catch (Exception e) {
			e.printStackTrace();
		}

		m_vecIsKaiPaiReady[wChairID] = 1;
		int cntTanPai = 0;
		for (int i = 0; i < 4; i++) {
			if (m_cbHandCardData[i][0] > 0) {
				cntTanPai++;
			}
		}
		SUBCMD_S_KaiPaiReady cmd = new SUBCMD_S_KaiPaiReady();
		cmd.byRound = m_byRound;
		for (int i = 0; i < 4; i++) {
			cmd.vecIsKaiPaiReady[i] = m_vecIsKaiPaiReady[i];
		}

		String sData = cmd.getJsonData();
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				SendData(m_pIUserItem[i].dwUserID, sData);
			}
		}
		if (m_bGameStarted) {
			if (cntTanPai == m_byMaxUser) {
				if (m_guize.equalsIgnoreCase("GUIZE_PT")) {
					KaiPai_PT();
				} else {
					KaiPai_HBL();
				}
				m_bGameStarted = false;
			}
		}
	}
	
	@Override
	public String getRoomInfo() {
		// TODO 自动生成的方法存根
				//m_lStartTime
				//m_pIUserItem
				// m_bTableStarted;
				// m_byRound;
				// m_byMaxRound;
				//m_dwTableID
				String roomInfo = String.format("%s/%s/%s/%s", 
						m_pIUserItem[0].GetUserInfo(), //#
						m_pIUserItem[1].GetUserInfo(), 
						m_pIUserItem[2].GetUserInfo(), 
						m_pIUserItem[3].GetUserInfo()
						);
				
				String[] vecParam = m_strParam.split(";");
				String vecParamStr = "";
				for (int i = 0; i < vecParam.length; i++) {
					String param = vecParam[i];

					if (param.equalsIgnoreCase("8JU")) {
						vecParamStr += "8局 ";
					}
					if (param.equalsIgnoreCase("16JU")) {
						vecParamStr += "16局 ";
					}

					if (param.equalsIgnoreCase("4REN"))
					{
						vecParamStr += "4人场 ";
					}
					if (param.equalsIgnoreCase( "3REN"))
					{
						vecParamStr += "3人场  ";
					}
					if (param.equalsIgnoreCase( "2REN"))
					{
						vecParamStr += "2人场  ";
					}
					if (param.equalsIgnoreCase("WUZHUANG")) {
						
						vecParamStr += "通比模式  ";
					}
					if (param.equalsIgnoreCase("DINGZHUANG")) {
						vecParamStr += "房主坐庄  ";
					}
					if (param.equalsIgnoreCase("LUNZHUANG")) {
						vecParamStr += "轮流坐庄  ";
					}
					if (param.equalsIgnoreCase("QIANGZHUANG")) {
						vecParamStr += "经典抢庄  ";
					}
					if (param.equalsIgnoreCase("GUIZE_HBL")) {
						vecParamStr += "红波浪模式";
					}
					if (param.equalsIgnoreCase("GUIZE_PT")) {
						vecParamStr += "普通模式 ";
					}
				}
				
				int start = 0;
				if(m_bTableStarted){
					start = 1;
				}
				String descInfo = String.format("十三张 %s",vecParamStr);
				roomInfo = String.format("%s|%d|%d|%d|%s|%d|%d|%d|%s|%s", roomInfo,m_dwTableID,m_lStartTime,start,descInfo,m_byMaxUser,m_byRound,m_byMaxRound,"十三张","SSZ");
				
				return roomInfo;
	}

	@Override
	public String getRoomDesc() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getPlayerCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getCostGem() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getCellScore() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getMinScore() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getStatus() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getPayway() {
		// TODO Auto-generated method stub
		return m_payWay;
	}

	@Override
	public String GetRoundInfo() {
		return "" + m_byRound + "/" +  m_byMaxRound;
	}
}
